
@Override
public void actionPerformed(ActionEvent e) {
    JMenuItem menuItem = (JMenuItem) e.getSource();

    if (menuItem.getName().equals("New Game")) {
    } else if (menuItem.getName().equals("Exit")) {
        windowClosing(null);
    } else if (menuItem.getName().equals("Statistics")) {
        showScore();
    } else if (menuItem.getName().equals("Beginner")) {
        this.difficulty = Difficulty.BEGINNER;
        newGameWithDifficulty(Difficulty.BEGINNER);
    } else if (menuItem.getName().equals("Intermediate")) {
        this.difficulty = Difficulty.INTERMEDIATE;
        newGameWithDifficulty(Difficulty.INTERMEDIATE);
    } else if (menuItem.getName().equals("Expert")) {
        this.difficulty = Difficulty.EXPERT;
        newGameWithDifficulty(Difficulty.EXPERT);
    }
}

public void newGame()
{                
    this.playing = false;        
    createBoard(difficulty);
    
    // 更新 UI 布局
    gui.updateUILayout(board.getRows(), board.getCols(), board.getNumberOfMines());
    
    gui.interruptTimer();
    gui.resetTimer();        
    gui.initGame();
    gui.setMines(board.getNumberOfMines());
}

// 新增方法：根据难度开始新游戏
private void newGameWithDifficulty(Difficulty difficulty)
{
    this.playing = false;
    createBoard(difficulty);
    
    // 更新 UI 布局
    gui.updateUILayout(difficulty.getRows(), difficulty.getCols(), difficulty.getMines());
    
    gui.initGame();
    gui.setMines(board.getNumberOfMines());
}

public Difficulty chooseDifficulty() {
    Object[] options = {"初级", "中级", "高级"};

    int choice = JOptionPane.showOptionDialog(null, "请选择难度:", "难度选择",
                JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

    switch (choice) {
        case 0: return Difficulty.BEGINNER;
        case 1: return Difficulty.INTERMEDIATE;
        case 2: return Difficulty.EXPERT;
        default: return Difficulty.BEGINNER; // 默认值
    }
}
